const ota = require('./utils/ota.js')
const util = require('./utils/util.js')

var scanning = false

//app.js
import Bluetooth from './utils/bluetooth'
App({
    globalData: {},
    onLaunch: function () {
        
    },

    // 连接蓝牙设备
    connectDeviceBle(deviceId) {
        let _loadingCount = 1,
            title = '连接中...'
        wx.showLoading({
            title,
        })
        Bluetooth.createBLEConnection(deviceId).then((res) => {
            console.warn('连接成功')
            if (_loadingCount == 1) {
                _loadingCount = 0
                wx.hideLoading({}).then((res) => {
                    this.getDeviceServices(deviceId)
                })
            }
        }).catch((err) => {
            console.log('连接设备失败--原因', err)
        })
    },

    // 获取设备服务值(主服务) services
    getDeviceServices(deviceId) {
        let _loadingCount = 1,
            title = '获取服务中...',
            serviceId = ''
        wx.showLoading({
            title,
        })
        Bluetooth.getBLEDeviceServices(deviceId).then((res) => {
            console.warn('获取服务值成功')
            console.warn('设备所有服务值', res.services)
            res.services.forEach((item) => {
                if (item.uuid.includes('FF00') && item.isPrimary) { // FF00--矫姿设备主服务
                    serviceId = item.uuid
                    if (_loadingCount == 1) {
                        _loadingCount = 0
                        wx.hideLoading({}).then((res) => {
                            this.getDeviceCharacteristics(deviceId, serviceId)
                        })
                    }
                }
            })
        }).catch((err) => {
            console.log('获取设备服务失败--原因', err)
        })
    },

    // 获取设备所有特征值--characteristics(读/写特征值)
    getDeviceCharacteristics(deviceId, serviceId) {
        let _loadingCount = 1,
            title = '读取特征值...',
            characteristicId = ''
        wx.showLoading({
            title,
        })
        Bluetooth.getBLEDeviceCharacteristics(deviceId, serviceId).then((res) => {
            console.warn('获取到设备主服务下的所有特征值', res.characteristics)
            res.characteristics.forEach((item) => {
                characteristicId = item.uuid
                if (_loadingCount == 1) {
                    _loadingCount = 0
                    wx.hideLoading({}).then((res) => {
                        console.log('设备连接成功')
                        
                    })
                }
                if (item.properties.notify) { // 读--特征值
                    console.log('读-特征值', characteristicId)
                    // this.notifyDeviceCharacteristicValueChange(deviceId, serviceId, characteristicId)
                }
                if (item.properties.write) { // 写--特征值
                    console.log('写-特征值', characteristicId)
                    this.setData({
                        deviceId,
                        serviceId,
                        characteristicId
                    })
                    console.log('设备信息', this.data)
                }
            })
        }).catch((err) => {
            console.log('读取设备特征值(读/写)失败--原因', err)
        })
    },

})